package com.leo.blog.web.headline;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.leo.blog.login.SecurityUtils;
import com.leo.blog.login.SelfUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dto.HeadLineExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.entity.HeadLine;
import com.leo.blog.enums.headLineStateEnum;
import com.leo.blog.service.HeadLineService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.HttpServletRequestUtil;
import com.leo.blog.util.subStringUtil;

@Controller
@RequestMapping(value = "/headline")
public class HeadLineManagementController {
	@Autowired
	private HeadLineService headLineService;
	
	@RequestMapping(value = "/getheadlinelistbypage")
	@ResponseBody
	public Map<String, Object> getHeadLineListByPage(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
				
		//获取前台传过来的值
		int typeNum = HttpServletRequestUtil.getInt(request, "typeNum");
		int status = HttpServletRequestUtil.getInt(request, "status");
		String headLineTitle = HttpServletRequestUtil.getString(request, "headLineTitle");
		
		HeadLine headLineCondition = new HeadLine();
		if(typeNum != 0) {
			headLineCondition.setTypeNum(typeNum);
		}
		if(status != 0) {
			headLineCondition.setStatus(status);
		}
		if(headLineTitle != null && headLineTitle != "") {
			headLineCondition.setHeadLineTitle(headLineTitle);
		}
		
		HeadLineExecution he = headLineService.getHeadLineListByPage(headLineCondition, pageIndex, pageSize);
		if(he.getState() == headLineStateEnum.SUCCESS.getState()) {
			//返回页数
			int page = 0;
			int pageCount = he.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			modelMap.put("headLineList", he.getHeadLineList());
			modelMap.put("count", he.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);		
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deleteheadlinebyid")
	@ResponseBody
	public Map<String, Object> deleteHeadLineById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int headLineId = HttpServletRequestUtil.getInt(request, "headLineId");
		
		Bloger bloger = SecurityUtils.getBloger();
		
		if(bloger.getHasPermission() == 1) {
			if(headLineId > 0) {
				HeadLineExecution he = headLineService.deleteHeadLineById("删除头条", headLineId);
				if(he.getState() == headLineStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "删除失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败,id错误");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		
		return modelMap;
	}
	
	@RequestMapping(value = "/updateheadline")
	@ResponseBody
	public Map<String, Object> updateHeadLine(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		
		//获取前台传过来的值
		int headLineId = HttpServletRequestUtil.getInt(request, "headLineId");
		String headLineTitle = HttpServletRequestUtil.getString(request, "headLineTitle");
		String headLineLink = HttpServletRequestUtil.getString(request, "headLineLink");
		String headLineImage = HttpServletRequestUtil.getString(request, "headLineImage");
		int priority = HttpServletRequestUtil.getInt(request, "priority");
		int typeNum = HttpServletRequestUtil.getInt(request, "typeNum");
		int status = HttpServletRequestUtil.getInt(request, "status");
		
		HeadLine headLine = new HeadLine();
		if(headLineId > 0) {
			headLine.setHeadLineId(headLineId);
		}
		if(headLineTitle != null && headLineTitle != "") {
			headLine.setHeadLineTitle(headLineTitle);
		}
		if(headLineLink != null && headLineLink != "") {
			headLine.setHeadLineLink(headLineLink);
		}
		if(headLineImage != null && headLineImage != "") {
			headLine.setHeadLineImage(headLineImage);
		}
		if(priority > -1) {
			headLine.setPriority(priority);
		}
		if(typeNum != 0) {
			headLine.setTypeNum(typeNum);
		}
		if(status != 0) {
			headLine.setStatus(status);
		}

		Bloger bloger = SecurityUtils.getBloger();
		
		if(bloger.getHasPermission() == 1) {
			HeadLineExecution he = headLineService.updateHeadLine("更新头条", headLine);
			if(he.getState() == headLineStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新头条失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getheadlinebyid")
	@ResponseBody
	public Map<String, Object> getHeadLineById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int headLineId = HttpServletRequestUtil.getInt(request, "headLineId");
		
		if(headLineId > 0) {
			HeadLineExecution he = headLineService.getHeadLineById(headLineId);
			if(he.getState() == headLineStateEnum.SUCCESS.getState()) {
				modelMap.put("headLine", he.getHeadLine());
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "获取头条失败,id错误");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "获取头条失败,id错误");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/addheadline")
	@ResponseBody
	@Transactional
	public Map<String, Object> addHeadLine(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String headLineTitle = HttpServletRequestUtil.getString(request, "headLineTitle");
		String headLineLink = HttpServletRequestUtil.getString(request, "headLineLink");
		String headLineImage = HttpServletRequestUtil.getString(request, "headLineImage");
		int priority = HttpServletRequestUtil.getInt(request, "priority");
		int typeNum = HttpServletRequestUtil.getInt(request, "typeNum");
		int status = HttpServletRequestUtil.getInt(request, "status");
		
		HeadLine headLine = new HeadLine();
		if(headLineTitle != null && headLineTitle != "") {
			headLine.setHeadLineTitle(headLineTitle);
		}
		if(headLineLink != null && headLineLink != "") {
			headLine.setHeadLineLink(headLineLink);
		}
		if(headLineImage != null && headLineImage != "") {
//			String addr = subStringUtil.subImageInsert(headLineImage);
//			String imageAddr = "/" + addr;
			headLine.setHeadLineImage(headLineImage);
		}
		if(priority > -1) {
			headLine.setPriority(priority);
		}
		if(typeNum != 0) {
			headLine.setTypeNum(typeNum);
		}
		if(status != 0) {
			headLine.setStatus(status);
		}

		Bloger bloger=SecurityUtils.getBloger();
		
		if(bloger.getHasPermission() == 1) {
			HeadLineExecution he = headLineService.addHeadLine("添加头条", headLine);
			if(he.getState() == headLineStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "添加头条失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		return modelMap;
	}
}
